package com.huatai.vba.finrecon.quartz;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.huatai.vba.common.utils.CalendarUtil;
import com.huatai.vba.dal.model.VbaFileTaskRecord;
import com.huatai.vba.finrecon.threadpool.ActionRunnable;
import com.huatai.vba.finrecon.threadpool.ThreadPool;
import com.huatai.vba.service.*;

/**
 * 自动执行任务
 * 
 * @author fanrui16
 *
 */
// @Component
public class AutoExecuteTaskAction extends AbstractTaskAction {

	@Autowired
	private VbaFileActionService vbaFileActionService;

	@Autowired
	private VbaFileTaskService vbaFileTaskService;

	@Autowired
	private VbaFileTaskRecordService vbaFileTaskRecordService;

	// 10 分钟执行一次
	// @Scheduled(cron = "0 10 * * * ?")
	public void execute() throws Exception {

		String occurDate = CalendarUtil.getStrDate(System.currentTimeMillis(), CalendarUtil.FORMAT4);
		// 查询未处理的记录
		List<VbaFileTaskRecord> vbaFileTaskRecords = vbaFileTaskRecordService.queryByOccurDateAndStatus(occurDate, "0");
		// 查询执行失败的记录
		List<VbaFileTaskRecord> vbaFileTaskRecords1 = vbaFileTaskRecordService.queryByOccurDateAndStatus(occurDate,
				"8");
		vbaFileTaskRecords.addAll(vbaFileTaskRecords1);
		for (VbaFileTaskRecord record : vbaFileTaskRecords) {
			ActionRunnable task = new ActionRunnable(vbaFileActionService, vbaFileTaskService, vbaFileTaskRecordService,
					record.getRecordId());
			ThreadPool.runTask(task);
		}

	}

}
